package com.danguo.happymonkey;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.support.v4.view.VelocityTrackerCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;

public class LessonOneGLSurfaceView extends GLSurfaceView
{
   private LessonOneRenderer theRenderer;
   private Context theParent;
   private VelocityTracker mVelocityTracker = null;

   public LessonOneGLSurfaceView( Context theContext )
   {
      super( theContext );

      theParent = theContext;
      this.setEGLContextClientVersion( 2 );
      theRenderer = new LessonOneRenderer( this );
      this.setRenderer( theRenderer );
   }

   public LessonOneGLSurfaceView( Context theContext, AttributeSet theAttrs )
   {
      super( theContext, theAttrs );
      theParent = theContext;
      this.setEGLContextClientVersion( 2 );
      theRenderer = new LessonOneRenderer( this );
      this.setRenderer( theRenderer );
   }

   @Override
   public boolean onTouchEvent( MotionEvent anEvent )
   {
      if( anEvent != null )
      {

         int point1 = theRenderer.getSelection( anEvent.getX(), anEvent.getY(),
               getHeight(), getWidth() );
         ((LessonOneActivity) theParent).setSelected( point1 );

         int index = anEvent.getActionIndex();
         int action = anEvent.getActionMasked();
         int pointerId = anEvent.getPointerId( index );

         switch( action )
            {
            case MotionEvent.ACTION_DOWN:
               if( mVelocityTracker == null )
               {
                  // Retrieve a new VelocityTracker object to watch the velocity
                  // of a motion.
                  mVelocityTracker = VelocityTracker.obtain();
               } else
               {
                  // Reset the velocity tracker back to its initial state.
                  mVelocityTracker.clear();
               }
               // Add a user's movement to the tracker.
               mVelocityTracker.addMovement( anEvent );
               break;
            case MotionEvent.ACTION_MOVE:
               mVelocityTracker.addMovement( anEvent );
               // When you want to determine the velocity, call
               // computeCurrentVelocity(). Then call getXVelocity()
               // and getYVelocity() to retrieve the velocity for each pointer
               // ID.
               mVelocityTracker.computeCurrentVelocity( 5 );
               // Log velocity of pixels per second
               // Best practice to use VelocityTrackerCompat where possible.
               float x = VelocityTrackerCompat.getXVelocity( mVelocityTracker,
                     pointerId );
               Log.d( "", "X velocity: " + x );
               theRenderer.setCameraRotation( x, 0 );
               Log.d( "", "Y velocity: " +
                           VelocityTrackerCompat.getYVelocity(
                                 mVelocityTracker, pointerId ) );
               break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
               // Return a VelocityTracker object back to be re-used by others.
               mVelocityTracker.recycle();
               break;
            }
         return true;
      } else
      {
         return super.onTouchEvent( anEvent );
      }
   }
}
